package com.chuangshu.chuangshuteam.mapper;

import com.chuangshu.chuangshuteam.entity.Users;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author k
 * @since 2025-09-21
 */
@Mapper
public interface UsersMapper extends BaseMapper<Users> {
    /**
     * 根据团队ID查询用户列表
     */
    @Select("SELECT u.* FROM users u " +
            "INNER JOIN team_members tm ON u.id = tm.user_id " +
            "WHERE tm.team_id = #{teamId} AND u.deleted_at IS NULL")
    List<Users> selectUsersByTeamId(@Param("teamId") Long teamId);

    /**
     * 根据角色查询用户列表
     */
    @Select("SELECT u.* FROM users u " +
            "INNER JOIN team_members tm ON u.id = tm.user_id " +
            "INNER JOIN roles r ON tm.role_id = r.id " +
            "WHERE r.name = #{role} AND u.deleted_at IS NULL")
    List<Users> selectUsersByRole(@Param("role") String role);

    /**
     * 根据openid查询用户（包含已软删除）
     */
    @Select("SELECT * FROM users WHERE wx_openid = #{wxOpenid} LIMIT 1")
    Users selectByOpenidIncludeDeleted(@Param("wxOpenid") String wxOpenid);
}
